﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NHibernate;

namespace ACompany.OrderProcessing.NHibernateRepository
{
    public class NHibernateHttpModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Application_BeginRequest);
            context.EndRequest += new EventHandler(Application_EndRequest);
        }

        private void Application_EndRequest(object sender, EventArgs e)
        {
            ISession session = NHibernate.Context.CurrentSessionContext.Unbind(SessionFactory.GetSessionFactory());
            session.Close();
            session.Dispose();
        }

        void Application_BeginRequest(object sender, EventArgs e)
        {
            NHibernate.Context.CurrentSessionContext.Bind(SessionFactory.GetNewSession());
        }


        public void Dispose()
        {
        }
    }
}
